package v1

import (
	"fmt"
	"ksirGo/controller/tools/response"
	"ksirGo/global"
	"ksirGo/utils"

	"github.com/gin-gonic/gin"
	"github.com/mojocn/base64Captcha"
)

// 获取图片验证码接口

func GetCaptcha(c *gin.Context) {
	captchaConfig := global.CONFIG.Captcha
	var config = utils.ConfigType{
		CaptchaType: "digit", // digit chinese math string audio
		DriverDigit: &base64Captcha.DriverDigit{
			Width:  captchaConfig.ImgWidth,
			Height: captchaConfig.ImgHeight,
			Length: captchaConfig.Length,
		},
	}

	if id, b64s, err := utils.GenerateCaptcha(config); err != nil {
		response.FailWithMessage(c, err.Error())
	} else {
		response.SuccessWithData(c, gin.H{
			"id":   id,
			"b64s": b64s,
		})
	}
}

// 获取注册短信验证码
type getCodeParamsType struct {
	Phone string `form:"phone" json:"phone" binding:"required"`
	// 类型： register(注册)；login(登录);reset_password(重置密码)
	Type string `form:"type" json:"type" binding:"required"`
}

func GetCode(c *gin.Context) {
	var params getCodeParamsType
	if err := c.ShouldBindQuery(&params); err != nil {
		response.FailWithMessage(c, "请检查信息是否填写完整！")
	} else {
		// 生成验证码
		if gin.Mode() == gin.ReleaseMode {
			// 生产环境使用验证码服务
			// todo
		} else {
			// 其他环境使用Redis模拟短信服务，节省开支
			response.SuccessWithData(c, gin.H{
				"data": utils.CreateMessageCode(fmt.Sprintf("%s-%s", params.Type, params.Phone)),
			})
		}
	}
}
